-module(schema).

-export([install/1, install/0]).

-include("schema.hrl").
-include("common.hrl").
-include("pp.hrl").

install() ->
    install([node()]).

install(Nodes) when is_list(Nodes) ->
    mnesia:stop(),
    mnesia:delete_schema(Nodes),
    catch(mnesia:create_schema(Nodes)),
    mnesia:start(),
    install_counter(Nodes),
    install_player_info(Nodes),
    install_player(Nodes),
    reset_counters(),
    ok.

install_player_info(Nodes) ->
    %% static player info
    {atomic, ok} =
        mnesia:create_table(tab_player_info,
                            [
                             {disc_copies, Nodes},
                             {index, [nick]},
                             {type, set},
                             {attributes, record_info(fields, tab_player_info)}
                            ]).

install_player(Nodes) ->
    %% player
    {atomic, ok} =
        mnesia:create_table(tab_player,
                            [
                             {ram_copies, Nodes},
                             {type, set},
                             {attributes, record_info(fields, tab_player)}
                            ]).


install_counter(Nodes) ->
    %% counter
    {atomic, ok} =
        mnesia:create_table(tab_counter,
                            [
                             {disc_copies, Nodes},
                             {type, set},
                             {attributes, record_info(fields, tab_counter)}
                            ]).


reset_counters()->
    counter:reset(player),
    ok.